package com.example.demo.service.impl;

import com.example.demo.service.AqsDemoService;
import com.example.demo.thread.current.AqsLock;
import org.springframework.stereotype.Service;

/**
 * @author Code Farmer
 * @date 2020/5/28 22:54
 */
@Service
public class AqsDemoServiceImpl implements AqsDemoService {

    AqsLock aqsLock = new AqsLock();

    private static int stock = 5;

    @Override
    public String decStockNoLock() {

        aqsLock.lock();

        //一个查询操作

        //一个更新操作
        if (stock > 0) {
            stock--;
            System.out.println("Thread :" + Thread.currentThread().getId() + "下单成功 ----------- 此时还有库存:" + stock);
        } else {
            System.out.println("Thread :" + Thread.currentThread().getId() + "下单失败");
        }

        aqsLock.unlock();
        return null;

    }
}
